package com.zjcloud.jwgl.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 排课结果实体类
 */
public class ScheduleResult implements Serializable {
    private static final long serialVersionUID = 1L;

    /** 排课结果ID */
    private String id;

    /** 课程安排列表 */
    private List<CourseArrangement> arrangements;

    /** 适应度分数 */
    private double fitnessScore;

    /** 是否存在冲突 */
    private boolean hasConflicts;

    /** 冲突详情 */
    private List<String> conflictDetails;

    public ScheduleResult(ScheduleGene gene) {
        this.id = UUID.randomUUID().toString();
        this.arrangements = gene.getArrangements();
        this.fitnessScore = gene.getFitness();
        this.hasConflicts = false;
        this.calculateConflicts();
    }

    public void moveCourse(int sourceSlot, int targetSlot) {
        if (sourceSlot >= 0 && sourceSlot < arrangements.size() 
            && targetSlot >= 0 && targetSlot < arrangements.size()) {
            CourseArrangement temp = arrangements.get(sourceSlot);
            arrangements.set(sourceSlot, arrangements.get(targetSlot));
            arrangements.set(targetSlot, temp);
            calculateConflicts();
        }
    }

    private void calculateConflicts() {
        // 初始化冲突列表
        this.conflictDetails = new ArrayList<>();
        this.hasConflicts = false;
        
        // 使用Map存储时间占用情况
        Map<String, List<CourseArrangement>> teacherTimeMap = new HashMap<>();
        Map<String, List<CourseArrangement>> classroomTimeMap = new HashMap<>();
        Map<String, List<CourseArrangement>> classTimeMap = new HashMap<>();
        
        // 遍历所有课程安排，检测冲突
        for (CourseArrangement arrangement : arrangements) {
            // 跳过状态不正常的课程安排
            if (arrangement.getStatus() != null && !"0".equals(arrangement.getStatus())) {
                continue;
            }
            
            // 构建时间键（星期几+第几节课）
            String timeKey = arrangement.getWeekDay() + "-" + arrangement.getLessonNo();
            
            // 1. 检测教师时间冲突
            String teacherKey = arrangement.getTeacherId() + "-" + timeKey;
            if (!teacherTimeMap.containsKey(teacherKey)) {
                teacherTimeMap.put(teacherKey, new ArrayList<>());
            }
            teacherTimeMap.get(teacherKey).add(arrangement);
            if (teacherTimeMap.get(teacherKey).size() > 1) {
                this.hasConflicts = true;
                this.conflictDetails.add("教师冲突：教师ID=" + arrangement.getTeacherId() 
                    + "在星期" + arrangement.getWeekDay() 
                    + "第" + arrangement.getLessonNo() + "节有多门课程安排");
            }
            
            // 2. 检测教室时间冲突
            String classroomKey = arrangement.getClassroomId() + "-" + timeKey;
            if (!classroomTimeMap.containsKey(classroomKey)) {
                classroomTimeMap.put(classroomKey, new ArrayList<>());
            }
            classroomTimeMap.get(classroomKey).add(arrangement);
            if (classroomTimeMap.get(classroomKey).size() > 1) {
                this.hasConflicts = true;
                this.conflictDetails.add("教室冲突：教室ID=" + arrangement.getClassroomId() 
                    + "在星期" + arrangement.getWeekDay() 
                    + "第" + arrangement.getLessonNo() + "节有多门课程安排");
            }
            
            // 3. 检测班级时间冲突
            String classKey = arrangement.getClassId() + "-" + timeKey;
            if (!classTimeMap.containsKey(classKey)) {
                classTimeMap.put(classKey, new ArrayList<>());
            }
            classTimeMap.get(classKey).add(arrangement);
            if (classTimeMap.get(classKey).size() > 1) {
                this.hasConflicts = true;
                this.conflictDetails.add("班级冲突：班级ID=" + arrangement.getClassId() 
                    + "在星期" + arrangement.getWeekDay() 
                    + "第" + arrangement.getLessonNo() + "节有多门课程安排");
            }
        }
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public List<CourseArrangement> getArrangements() {
        return arrangements;
    }

    public void setArrangements(List<CourseArrangement> arrangements) {
        this.arrangements = arrangements;
    }

    public double getFitnessScore() {
        return fitnessScore;
    }

    public void setFitnessScore(double fitnessScore) {
        this.fitnessScore = fitnessScore;
    }

    public boolean hasConflicts() {
        return hasConflicts;
    }

    public void setHasConflicts(boolean hasConflicts) {
        this.hasConflicts = hasConflicts;
    }

    public List<String> getConflictDetails() {
        return conflictDetails;
    }

    public void setConflictDetails(List<String> conflictDetails) {
        this.conflictDetails = conflictDetails;
    }
}